﻿#pragma once
#include "oatpp/core/Types.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "oatpp/encoding/Base64.hpp"

#include OATPP_CODEGEN_BEGIN(DTO) ///< Begin DTO codegen section

class User : public oatpp::DTO {

	DTO_INIT(User, DTO /* extends */)

	DTO_FIELD(String, name, "First-Name");
	DTO_FIELD(String, surname, "Family-Name");
	DTO_FIELD(Int32, age);
	DTO_FIELD(Fields<List<Object<User>>>, familyMembers); ///< Map<String, List<User>>
	DTO_FIELD(Fields<String>, additionalNotes); ///< Map<String, String>

};

class KVMetadata : public oatpp::DTO {

    DTO_INIT(KVMetadata, DTO)

    /**
        * Full path of the entry.
        */
    DTO_FIELD(String, key, "Key");

    DTO_FIELD(Boolean, serviceEnableTagOverride, "ServiceEnableTagOverride");

    /**
     * Value. <br>
     * May contain base64 encoded value if no "raw" parameter were specified in request ex.:`"v1/kv/{key}?raw"`.
     */
    DTO_FIELD(String, value, "Value");

    /**
     * Session.
     */
    DTO_FIELD(String, session, "Session");

    /**
     * Internal index value that represents when the entry was created.
     */
    DTO_FIELD(Int32, createIndex, "CreateIndex");

    /**
     * Last index that modified this key.
     */
    DTO_FIELD(Int32, modifyIndex, "ModifyIndex");

    /**
     * The number of times this key has successfully been acquired in a lock.
     * If the lock is held, the Session key provides the session that owns the lock.
     */
    DTO_FIELD(Int32, lockIndex, "LockIndex");

    /**
     * An opaque unsigned integer that can be attached to each entry.
     */
    DTO_FIELD(Int32, flags, "Flags");

private:
    mutable String m_valueDecoded = nullptr;
public:

    /**
     * Get base64 decoded &l:KVMetadata::value;.
     * @return - decoded &l:KVMetadata::value;.
     */
    String getValueDecoded() const {
        if (!m_valueDecoded) {
            if (value) {
                m_valueDecoded = oatpp::encoding::Base64::decode(value);
            }
            else {
                throw std::runtime_error("[oatpp::consul::rest::KVMetadata::getValueDecoded()]: Value is null. Can't decode value");
            }
        }
        return m_valueDecoded;
    }
};

#include OATPP_CODEGEN_END(DTO) ///< End DTO codegen section